package de.voichie.android.wordthous.views;

import de.voichie.android.wordthous.R;
import de.voichie.android.wordthous.data.Field;
import de.voichie.android.wordthous.data.FieldAttribute;
import de.voichie.android.wordthous.data.SingleField;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class FieldView extends View implements OnTouchListener
{

	private Field field = new Field();
	
//	boolean isInitialized;
	Paint paint = new Paint();

	public FieldView(Context context)
	{
		super(context);
		setFocusable(true);
		setFocusableInTouchMode(true);

		this.setOnTouchListener(this);

		paint.setColor(Color.GRAY);
		paint.setAntiAlias(true);
		paint.setStyle(Style.FILL);
		
//		isInitialized = false;
	}

	private void init()
	{


	}
	
	@Override
	public void onDraw(Canvas canvas)
	{
		int offset = 4;

		drawField(canvas, this.field);
		
		// characters
		for (int i = 0; i < 7; i++) {
			
			// draw tile
			canvas.drawRect(45 * i + offset, offset + 615 , 45 * (i+1) + offset - 1, 45 + offset + 615, paint);
			// set letters to tile
			int resetColor = paint.getColor();
			paint.setColor(Color.BLACK);
			paint.setTextSize(30);
			canvas.drawText("A", 45 * i + 15, 655, paint);
			paint.setColor(resetColor);
		}
		
		
		
			
			 
	}
	
	private void drawField (Canvas canvas, Field field) {
		
		SingleField [] [] arrayFields = field.arrayFields;
 		
		for (int i = 0; i < 15; i++) {
			
			for (int j = 0; j < 15; j++) {
				drawSingleField(canvas, arrayFields[i][j]);
				
			}
			
		}
	}
	
	private void drawSingleField (Canvas canvas, SingleField field) {
		int resetColor = paint.getColor();
		String singleFieldText [] = null;
		
		switch (field.getAttribute()) {
		case START_FIELD:
			paint.setColor(Color.GREEN);
			singleFieldText = new String[] {"Start"};
			break;
		case DOUBLE_LETTER_FIELD:
			paint.setColor(Color.CYAN);
			singleFieldText = new String[] {"Double" , "Letter", "Value"};
			break;
		case THIRD_LETTER_FIELD:
			paint.setColor(Color.BLUE);
			singleFieldText = new String[] {"Third" , "Letter", "Value"};
			break;
		case DOUBLE_WORD_FIELD:
			paint.setColor(Color.MAGENTA);
			singleFieldText = new String[] {"Double" , "Word", "Value"};
			break;
		case THIRD_WORD_FIELD:
			paint.setColor(Color.RED);
			singleFieldText = new String [] {"Third" , "Word", "Value"};
			break;
		default:
			break;
		}
		canvas.drawRect(field.getxPos(), field.getyPos() , field.getxPos() + field.getSize(), field.getyPos() + field.getSize(), paint);
		paint.setColor(Color.BLACK);
		paint.setTextSize(8);
		if (singleFieldText != null && singleFieldText.length == 1) {
			canvas.drawText(singleFieldText[0], field.getxPos() + 10, field.getyPos() + 25, paint);
		} else if (singleFieldText != null && singleFieldText.length == 3) {
			if (singleFieldText[0].equals("Double"))
				canvas.drawText(singleFieldText[0], field.getxPos() + 7, field.getyPos() + 13, paint);
			else
				canvas.drawText(singleFieldText[0], field.getxPos() + 10, field.getyPos() + 13, paint);
			canvas.drawText(singleFieldText[1], field.getxPos() + 10, field.getyPos() + 22, paint);
			canvas.drawText(singleFieldText[2], field.getxPos() + 10, field.getyPos() + 30, paint);
		}
		
		
		paint.setColor(resetColor);
		
		
		
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		return false;
	}


}
